#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/config/SourcePrefix.h>
#include "zkemsdk.h"
#include "udktest_stdapi.h"
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( UDKStdApiTest );

static char *valid_ip=(char*)"192.168.0.126";
static char *invalid_ip=(char*)"192.168.1.1";
static int valid_port=4370;
#ifndef WIN32
typedef struct {
UDK_WORD wYear;
UDK_WORD wMonth;
UDK_WORD wDayOfWeek;
UDK_WORD wDay;
UDK_WORD wHour;
UDK_WORD wMinute;
UDK_WORD wSecond;
UDK_WORD wMilliseconds;
}SYSTEMTIME;
#endif

void UDKStdApiTest::testConnect(){
	//nothing to test here
}


#define A_PIN_CHECK_IN		0
#define A_PIN_CHECK_OUT		1
#define A_PIN_BREAK_OUT		2
#define A_PIN_BREAK_IN		3
#define A_PIN_OT_IN			4
#define A_PIN_OT_OUT		5
typedef struct strAccLinkParamData 
{
	char szUser[32];		//User Name
	SYSTEMTIME stStTime;	//Start Time
	SYSTEMTIME stEdTime;	//End Time
	int nPinType;			//A_PIN_CHECK_IN or A_PIN_CHECK_OUT
	strAccLinkParamData()
	{
		memset(szUser, 0, 32);
		memset(&stStTime, 0, sizeof(SYSTEMTIME));
		memset(&stEdTime, 0, sizeof(SYSTEMTIME));
		nPinType = 0;
	}

}STRALParamData, *PSTRALParamData;

int OnAttTransaction( char* szEnrollNumber, UDK_S32 IsInValid, UDK_S32 AttState, UDK_S32 VerifyMethod, UDK_S32 Year, UDK_S32 Month, UDK_S32 Day, UDK_S32 Hour, UDK_S32 Minute, UDK_S32 Second )
{
	static const char* pintypes[]={
		"CheckIn",
		"CheckOut",
		"BreakOut",
		"BreakIn",
		"OT-In",
		"OT-Out",
		"Unknown",
	};
	UDK_LOGD("%04d-%02d-%02d %02d:%02d:%02d %s %s [%d]\n",Year,Month,Day,Hour,Minute,Second,szEnrollNumber,pintypes[AttState],VerifyMethod);
	return 0;	
}

static void ReadCallBack(void* cookie, int DataType, int nDataSize, char* DataBuffer)
{        
	UDK_LOGD("datatype %d arrived\n",DataType);
	UDK_DUMP("Got data",DataBuffer,nDataSize);
        char UserNo[25] = {0};
        switch(DataType)
        {
        case EF_ATTLOG:{
			memcpy(UserNo, DataBuffer, 24);
			OnAttTransaction(UserNo, DataBuffer[25]>>4, DataBuffer[25] & 0xF, DataBuffer[24], 
				DataBuffer[26]+2000, DataBuffer[27], DataBuffer[28], DataBuffer[29], DataBuffer[30], DataBuffer[31]);
			}break;
        case EF_ALARM:
                break;
        case EF_FINGER:
                break;
        default:
                break;
        }
}
//test robust APIs in one case
void UDKStdApiTest::testConnectTCP(){
  	UDK_HANDLE h;
  	int ret;	
	int port = 4370;
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	
	int m_nVerifyMthod;
	int m_PINWidth;
	int m_IsTFTMachine=0;
	int nRet = Z_RegEvent(h, 0xFFFF);
	int nValue = 0;
	if (Z_IsTFTMachine(h, &nValue))
	{
		m_IsTFTMachine = nValue;
	}
	char sVerMthod[10] = {0} ;
	Z_GetSysOption(h, (char*)"~UserExtFmt", sVerMthod);
	m_nVerifyMthod = atoi(sVerMthod);
	Z_GetPINWidth(h, &m_PINWidth);	
	UDK_LOGD("m_IsTFTMachine=%d m_nVerifyMthod=%d\n",m_IsTFTMachine,m_nVerifyMthod);
	ret = Z_Close(h);	
	CPPUNIT_ASSERT(ret==Z_ERROR_OK);
}

void UDKStdApiTest::testZ_Connect_NETEX()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	CPPUNIT_ASSERT( Z_Close(h)==Z_ERROR_OK);
	CPPUNIT_ASSERT(Z_Connect_NETEX(valid_ip,80,(void*)ReadCallBack,NULL,0)==0);
	CPPUNIT_ASSERT(Z_Connect_NETEX(invalid_ip,valid_port,(void*)ReadCallBack,NULL,0)==0);
	CPPUNIT_ASSERT(Z_Connect_NETEX(invalid_ip,80,(void*)ReadCallBack,NULL,0)==0);
}
void UDKStdApiTest::testZ_Connect_NET()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NET(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	CPPUNIT_ASSERT( Z_Close(h)==Z_ERROR_OK);
	CPPUNIT_ASSERT(Z_Connect_NET(valid_ip,80,(void*)ReadCallBack,NULL,0)==0);
	CPPUNIT_ASSERT(Z_Connect_NET(invalid_ip,valid_port,(void*)ReadCallBack,NULL,0)==0);
	CPPUNIT_ASSERT(Z_Connect_NET(invalid_ip,80,(void*)ReadCallBack,NULL,0)==0);
}

void UDKStdApiTest::testZ_RebootDevice()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	int ret = Z_RebootDevice(h);
	if(ret!=Z_ERROR_OK){
		CPPUNIT_FAIL("RebootDevice failed ?\n");
		UDK_LOGW("ret=%d\n",ret);
	}
	CPPUNIT_ASSERT( Z_Close(h)!=Z_ERROR_OK);
}
void UDKStdApiTest::testZ_Connect_P4P(){
  UDK_HANDLE h;	  	
  h = Z_Connect_P4P((char*)"KA8FR278AVPRFFGA111A",(void*)ReadCallBack,NULL,0);
  if(h==NULL){
    CPPUNIT_FAIL("p4p connect failed\n");
  }
  Z_Close(h);
}
void UDKStdApiTest::testZ_Beep()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	int ret = Z_Beep(h,100);	
	CPPUNIT_ASSERT(ret==Z_ERROR_OK);
	udk_msleep(2000);
	ret = Z_Beep(h,100);
	UDK_LOGW("testZ_Beep ret=%d\n",ret);
	CPPUNIT_ASSERT(ret==Z_ERROR_OK);
	CPPUNIT_ASSERT(Z_Close(h)==Z_ERROR_OK);
}

void UDKStdApiTest::testZ_GetSysOption()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	int ret;
	char commbuf[16] = {0} ;
	ret = Z_GetSysOption(h, (char*)"~UserExtFmt", commbuf);	
	CPPUNIT_ASSERT(ret==Z_ERROR_OK);
	ret = Z_GetSysOption(h, (char*)"InvalidOptionName", commbuf);
	CPPUNIT_ASSERT(ret!=Z_ERROR_OK);
	CPPUNIT_ASSERT(Z_Close(h)==Z_ERROR_OK);
}

void UDKStdApiTest::testZ_Unlock()
{
	UDK_HANDLE h;	  	
	h = Z_Connect_NETEX(valid_ip,valid_port,(void*)ReadCallBack,NULL,0);
	CPPUNIT_ASSERT(h!=0);
	int ret = Z_Unlock(h,100);
	CPPUNIT_ASSERT(ret==Z_ERROR_OK);
	CPPUNIT_ASSERT(Z_Close(h)==Z_ERROR_OK);
}


